package com.xywl.escort.view.treasure;

import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.ShoppingCartReq;
import com.xywl.escort.bean.ShoppingCartResp;
import com.xywl.escort.bean.ShoppingCartUpdateReq;
import com.xywl.escort.bean.ShoppingCartUpdateResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class TreasurePresenter implements TreasureContract.Presenter {
    private TreasureContract.View mView;
    private ApiServer mApiServer;

    @Inject
    TreasurePresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(TreasureContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadTreasureList(ShoppingCartReq req) {
        mApiServer.shoppingCartList(req)
                .compose(RxUtil.<ShoppingCartResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ShoppingCartResp>bindToLife())
                .subscribe(new BaseSubscriber<ShoppingCartResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadTreasureListFailure();
                    }

                    @Override
                    public void onNext(ShoppingCartResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadTreasureListSuccess(resp);
                        } else {
                            mView.loadTreasureListFailure();
                        }
                    }
                });
    }

    @Override
    public void delShoppingCart(String skuIds, final int position) {
        ShoppingCartReq req = new ShoppingCartReq();
        req.skuIds = skuIds;
        mApiServer.delShoppingCart(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.delShoppingCartFailure();
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.delShoppingCartSuccess(resp, position);
                        } else {
                            mView.delShoppingCartFailure();
                        }
                    }
                });
    }

    @Override
    public void delShoppingCarts(String skuIds) {
        ShoppingCartReq req = new ShoppingCartReq();
        req.skuIds = skuIds;
        mApiServer.delShoppingCart(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.delShoppingCartFailure();
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.delShoppingCartsSuccess(resp);
                        } else {
                            mView.delShoppingCartFailure();
                        }
                    }
                });
    }

    @Override
    public void updateShopCart(ShoppingCartUpdateReq req) {
        mApiServer.shoppingCartUpdate(req)
                .compose(RxUtil.<ShoppingCartUpdateResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ShoppingCartUpdateResp>bindToLife())
                .subscribe(new BaseSubscriber<ShoppingCartUpdateResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.updateShopCartFailure();
                    }

                    @Override
                    public void onNext(ShoppingCartUpdateResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.updateShopCartSuccess(resp);
                        } else {
                            mView.updateShopCartFailure();
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("TreasurePresenter").e("unSubscribe");
        mView = null;
    }
}
